package com.xmtlxx.web.controller.oms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xmtlxx.web.controller.oms.service.IStagingService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.oms.domain.bo.StagingBo;
import com.xmtlxx.oms.domain.bo.TaskDtlBo;
import com.xmtlxx.oms.domain.vo.StagingInfoVo;
import com.xmtlxx.oms.domain.vo.StagingVo;
import com.xmtlxx.oms.domain.vo.TaskDtlVo;
import com.xmtlxx.oms.service.ITaskDtlService;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.service.ISysDeptService;
import com.xmtlxx.system.service.ISysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 暂存登记Controller
 *
 * @author xmtlxx
 * @date 2022-11-20
 */
@Validated
@Api(value = "接收登记控制器", tags = {"接收登记管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/staging")
public class StagingController extends BaseController {

    private final IStagingService iStagingService;

    private final ITaskDtlService iTaskDtlService;

    private final ISysOssService iSysOssService;

    private final ISysDeptService deptService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<StagingVo> initBean(){
        StagingVo vo = new StagingVo();

        vo.setDeptId(LoginHelper.getDeptId());
        vo.setTransferName(LoginHelper.getLoginUser().getDeptName());
        vo.setTransferDept(LoginHelper.getDeptId());
        vo.setOfficeId(LoginHelper.getDeptId());
        vo.setOfficeName(LoginHelper.getLoginUser().getDeptName());
        vo.setRegisterer(LoginHelper.getNickname());
        vo.setRegisterTime(new Date());
        vo.setOss(new ArrayList<>());
        return R.ok(vo);
    }

    /**
     * 查询暂存登记列表
     */
    @ApiOperation("查询接收登记列表")
//    @SaCheckPermission("oms:staging:list")
    @GetMapping("/list")
    public TableDataInfo<StagingVo> list(@Validated(QueryGroup.class) StagingBo bo, PageQuery pageQuery) {
//        if(LoginHelper.getLoginUser().getRolePermission().contains("qxcangguang")){
//            bo.setCountyTransferStatus(bo.getTransferStatus());
//        }
        bo.setDelFlag("0");
        return iStagingService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询暂存登记列表
     */
    @ApiOperation("查询接收登记列表(新)")
    @GetMapping("/getStagingPageList")
    public TableDataInfo<StagingInfoVo> getStagingPageList(@Validated(QueryGroup.class) StagingBo bo, PageQuery pageQuery){
        return iStagingService.getStagingPageList(bo, pageQuery);
    }
    /**
     * 导出暂存登记列表
     */
    @ApiOperation("导出暂存登记列表")
    @SaCheckPermission("oms:staging:export")
    @Log(title = "接收登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated StagingBo bo, HttpServletResponse response) {
        List<StagingVo> list = iStagingService.queryList(bo);
        ExcelUtil.exportExcel(list, "暂存登记", StagingVo.class, response);
    }

    /**
     * 获取暂存登记详细信息
     */
    @ApiOperation("获取接收登记详细信息")
//    @SaCheckPermission("oms:staging:query")
    @GetMapping("/{id}")
    public R<StagingVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        StagingVo vo = iStagingService.queryById(id);
        vo.setTransferTime(new Date());
        TaskDtlBo bo = new TaskDtlBo();
        bo.setTaskId(vo.getId());
        List<TaskDtlVo> dtls = iTaskDtlService.queryList(bo);
        if(dtls != null && dtls.size() > 0){
            for (TaskDtlVo dtl:dtls){
                SysOssBo oss = new SysOssBo();
                oss.setObjid(dtl.getPno());
                oss.setBuss("staging");
                dtl.setOss(iSysOssService.selectSysOssList(oss));
            }
        }

        SysOssBo oss = new SysOssBo();
        oss.setObjid(vo.getId());
        oss.setBuss("staging");
        vo.setOss(iSysOssService.selectSysOssList(oss));

        vo.setDtls(dtls);
        return R.ok(vo);
    }

    /**
     * 获取暂存登记详细信息
     */
    @ApiOperation("获取接收登记详细信息(新)")
    @GetMapping("/getStagingInfoByPno")
    public R<StagingVo> getStagingInfoByPno(@NotNull(message = "主键不能为空")
                                                String pno){
        StagingVo vo = iStagingService.getStagingInfoByPno(pno);
        if(StrUtil.isNotBlank(vo.getId())){
            SysOssBo oss = new SysOssBo();
            oss.setObjid(vo.getId());
            oss.setBuss("staging");
            vo.setOss(iSysOssService.selectSysOssList(oss));
        }
        return R.ok(vo);
    }
    @ApiOperation("获取行政类涉案财物详细信息")
    @GetMapping("/getXzProByPno")
    public R<StagingVo> getXzProByPno(@NotNull(message = "主键不能为空") String pno){
        StagingVo vo = iStagingService.getXzProByPno(pno);
        SysOssBo oss = new SysOssBo();
        oss.setObjid(vo.getId());
        oss.setBuss("staging");
        vo.setOss(iSysOssService.selectSysOssList(oss));
        return R.ok(vo);
    }

    /**
     * 获取行政类涉案财物物品编码
     * @return
     */
    @ApiOperation("获取新增物品编码-行政类涉案财物")
    @GetMapping("/generatePno")
    public R<String> generatePno(){
        return R.ok("",iStagingService.getXzPno());
    }
    /**
     * 查询移交登记列表-行政类涉案财物
     */
    @ApiOperation("查询行政类涉案财物列表")
    @GetMapping("/getXzPros")
    public TableDataInfo<StagingInfoVo> getXzPros(@Validated(QueryGroup.class) StagingBo bo, PageQuery pageQuery){
        return iStagingService.getXzProList(bo, pageQuery);
    }
    /**
     * 新增暂存登记
     */
    @ApiOperation("新增暂存登记")
    @SaCheckPermission("oms:staging:add")
    @Log(title = "接收登记", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StagingVo vo) {
        StagingBo bo = new StagingBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iStagingService.inserStagingByBo(bo) ? 1 : 0);
    }

    /**
     * 修改暂存登记
     */
    @ApiOperation("修改暂存登记")
    @SaCheckPermission("oms:staging:edit")
    @Log(title = "接收登记", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StagingVo vo) {
        StagingBo bo = new StagingBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iStagingService.updateStagingByBo(bo) ? 1 : 0);
    }

    /**
     * 删除暂存登记
     */
    @ApiOperation("删除暂存登记")
    @SaCheckPermission("oms:staging:remove")
    @Log(title = "接收登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iStagingService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("删除接收登记")
    @PostMapping("/delete/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> delete(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String id) {
        return toAjax(iStagingService.deleteVoById(id));
    }

}
